<?php
namespace common\models\friend;

use common\models\Common;
use common\models\Functions;
use common\models\im\Im;
use common\models\group\Group;
use common\models\group\GroupUser;
use common\models\Status;
use common\models\user\User;
use Yii;
use yii\base\Exception;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "goods".
 *
 * @property int $id
 * @property string $name
 */
class FriendApply extends Common
{
    
    public static function tableName()
    {
        return '{{%friend_apply}}'; // TODO: Change the autogenerated stub
    }

    public function getUser()
    {
        return $this->hasOne(User::className(), ['id' => 'user_id']);
    }

    public function getFriend()
    {
        return $this->hasOne(User::className(), ['id' => 'friend_id']);
    }


    // 未处理的未读消息
    public static function notReadApplyNum($userId = 0, $groupId = 0, $greetLastTime = '', $interactLastTime = '', $interactContent = '') {

        $imModel = new Im();

        if ($groupId) {
            $groupUserWhere = $groupUserAndWhere = [];
            $groupUserWhere['group_id'] = $groupId;
            $groupUserWhere['user_type'] = [
                Status::GROUP_USER_TYPE_HOME,
                Status::GROUP_USER_TYPE_MANAGE,
            ];
            $groupUserList = GroupUser::getObjectByAndWhere($groupUserWhere, $groupUserAndWhere,['group_id','user_id','user_type','user_tx_code'])->with([
                'userApplyCount' => function ($q) {
                    $q->select(['COUNT(1) total','user_id']);
                },

                'groupApplyInviteCount' => function ($q) {
                   $q->select(['COUNT(1) total','group_id']);
                },

                'groupBeApplyInviteCount' => function ($q) {
                    $q->select(['COUNT(1) total','user_id']);
                },

                'circleLikeCount' => function ($q) {
                    $q->select(['COUNT(1) total','topic_user_id']);
                },

                'circleCollectCount' => function ($q) {
                    $q->select(['COUNT(1) total','topic_user_id']);
                },

                'attentionCount' => function ($q) {
                    $q->select(['COUNT(1) total','topic_id']);
                },

                'commentCount' => function ($q) {
                    $q->select(['COUNT(1) total','topic_user_id']);
                },

                'commentAtCount' => function ($q) {
                    $q->select(['COUNT(1) total','topic_user_id']);
                },

                'greetCount' => function ($q) {
                    $q->select(['COUNT(1) total','greet_user_id']);
                },

            ])->asArray()->all();

            foreach ($groupUserList as $key => $value) {
                $friendApply = ArrayHelper::getValue($value,'userApplyCount',[]);
                $groupInviteApply = ArrayHelper::getValue($value,'groupApplyInviteCount',[]);
                $groupBeInviteApply = ArrayHelper::getValue($value,'groupBeApplyInviteCount',[]);
                $circleLike = ArrayHelper::getValue($value,'circleLikeCount',[]);
                $circleCollect = ArrayHelper::getValue($value,'circleCollectCount',[]);
                $attention = ArrayHelper::getValue($value,'attentionCount',[]);
                $commentCount = ArrayHelper::getValue($value,'commentCount',[]);
                $commentAtCount = ArrayHelper::getValue($value,'commentAtCount',[]);
                $greetCount = ArrayHelper::getValue($value,'greetCount',[]);

                $friendApplyNum = $friendApply['total'] ?: 0;
                $groupApplyNum = (int) ( $groupInviteApply['total'] ?: 0 )+ ( $groupBeInviteApply['total'] ?: 0 );

                $circleLikeNum = $circleLike['total'] ?: 0;
                $circleCollectNum = $circleCollect['total'] ?: 0;
                $attentionNum = $attention['total'] ?: 0;
                $commentNum = $commentCount['total'] ?: 0;
                $commentAtNum = $commentAtCount['total'] ?: 0;
                $greetNum = $greetCount['total'] ?: 0;
                $imModel->sendNotReadApplyNum($value['user_tx_code'], $groupApplyNum, $friendApplyNum,$circleLikeNum,$circleCollectNum,$attentionNum,($commentNum + $commentAtNum),$greetNum);
            }
        } else {
            $imUserWhere = $imUserAndWhere = [];
            $imUserWhere['id'] = $userId;
            $imUser = User::getObjectByAndWhere($imUserWhere, $imUserAndWhere,['id','tx_code','user_code'])->with([
                'userApplyCount' => function ($q) {
                    $q->select(['COUNT(1) total','friend_id']);
                },

                'groupApplyInviteCount' => function ($q) use ($userId) {
                    $groupIds = GroupUser::getFormArray(['user_id' => $userId,'user_type' => [Status::GROUP_USER_TYPE_HOME, Status::GROUP_USER_TYPE_MANAGE]],'group_id','group_id');
                    $q->select(['COUNT(1) total','user_id','group_id'])->where(['group_id' => $groupIds]);
                },

                'groupBeApplyInviteCount' => function ($q) {
                    $q->select(['COUNT(1) total','user_id']);
                },

                'circleLikeCount' => function ($q) {
                    $q->select(['COUNT(1) total','topic_user_id']);
                },

                'circleCollectCount' => function ($q) {
                    $q->select(['COUNT(1) total','topic_user_id']);
                },

                'attentionCount' => function ($q) {
                    $q->select(['COUNT(1) total','topic_id']);
                },

                'commentCount' => function ($q) {
                    $q->select(['COUNT(1) total','topic_user_id']);
                },

                'commentAtCount' => function ($q) {
                    $q->select(['COUNT(1) total','topic_user_id']);
                },

                'greetCount' => function ($q) {
                    $q->select(['COUNT(1) total','greet_user_id']);
                },

            ])->asArray()->one();
            $friendApply = ArrayHelper::getValue($imUser,'userApplyCount',[]);
            $groupInviteApply = ArrayHelper::getValue($imUser,'groupApplyInviteCount',[]);
            $groupBeInviteApply = ArrayHelper::getValue($imUser,'groupBeApplyInviteCount',[]);
            $circleLike = ArrayHelper::getValue($imUser,'circleLikeCount',[]);
            $circleCollect = ArrayHelper::getValue($imUser,'circleCollectCount',[]);
            $attention = ArrayHelper::getValue($imUser,'attentionCount',[]);
            $commentCount = ArrayHelper::getValue($imUser,'commentCount',[]);
            $commentAtCount = ArrayHelper::getValue($imUser,'commentAtCount',[]);
            $greetCount = ArrayHelper::getValue($imUser,'greetCount',[]);

            $friendApplyNum = $friendApply['total'] ?: 0;
            $groupApplyNum = (int) ( $groupInviteApply['total'] ?: 0 )+ ( $groupBeInviteApply['total'] ?: 0 );
            $circleLikeNum = $circleLike['total'] ?: 0;
            $circleCollectNum = $circleCollect['total'] ?: 0;
            $attentionNum = $attention['total'] ?: 0;
            $commentNum = $commentCount['total'] ?: 0;
            $commentAtNum = $commentAtCount['total'] ?: 0;
            $greetNum = $greetCount['total'] ?: 0;
//            var_dump($groupApplyNum);
//            var_dump($friendApplyNum);die;

            $greetData = $interactData = [];
            if ($greetLastTime) {
                $greetData['last_time'] = $greetLastTime;
                $greetData['content'] = "{$greetNum}个人给你打招呼";
            }

            if ($interactLastTime) {
                $interactData['content'] = $interactContent;
                $interactData['last_time'] = $interactLastTime;
            }

            $imModel->sendNotReadApplyNum($imUser['tx_code'], $groupApplyNum, $friendApplyNum,$circleLikeNum,$circleCollectNum,$attentionNum,($commentNum + $commentAtNum),$greetNum,$interactData,$greetData);
        }
    }



}
